package com.service.hibernate.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.service.hibernate.pojos.Stocks;

public class StockViewDAO extends GenericDAO<Stocks> {

	// Overwrite method to get list for a specific id
	@SuppressWarnings("unchecked")
	public List<Stocks> getAllData(String pojo, String id) {
		List<Stocks> stk = new ArrayList<Stocks>();
		try {
			String query = "FROM " + pojo + " where item_id=" + id;
			stk = session.createQuery(query).list();
		} catch (Exception e) {
			System.out.println("Error in ServicesDAO - getServices() :");
			e.printStackTrace();
		}
		return stk;
	}

	// method to buy items for the stock
	public void saveOrUpdateStock(Stocks stk) {

		try {

			// stk.setQuantity(stk.getQuantity()+giveQuantity(stk));
			float value = stk.getQuantity();

			stk.setQuantity(value + 10);

			session.update(stk);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	public float giveQuantity(Stocks stk) {
		Stocks store = new Stocks();
		float itemQuantity;
		Iterator<Stocks> it = getAllData("stocks",
				Integer.toString(stk.getItem_id())).iterator();
		while (it.hasNext()) {
			store = it.next();
		}
		itemQuantity = store.getQuantity();
		return itemQuantity;
	}

}
